﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProductMonitor.MyUserControl
{
    /// <summary>
    /// RingUC.xaml 的交互逻辑
    /// </summary>
    public partial class RingUC : UserControl
    {
        public RingUC()
        {
            InitializeComponent();

            //窗体大小发生变化的事件
            SizeChanged += OnSizeChanged;

        }


        //圆环百分比的依赖属性
        public double PercentValue
        {
            get { return (double)GetValue(PercentValueProperty); }
            set { SetValue(PercentValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PercentValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PercentValueProperty =
            DependencyProperty.Register("PercentValue", typeof(double), typeof(RingUC));


   
        /// <summary>
        /// 窗体大小发生改变执行的方法
        /// </summary>
        private void OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            Drawing();
        }


        //绘制圆环的方法
        private void Drawing()
        {
            //取父级渲染器的长宽最小值
            LayoutGrid.Width = Math.Min(RenderSize.Width, RenderSize.Height);
            //定义圆环半径
            double radius = LayoutGrid.Width / 2;  

            //定义偏移量
            double offsetX = (PercentValue % 100 * 3.6 + 90) * Math.PI / 180;
            double offsetY = (PercentValue % 100 * 3.6 - 90) * Math.PI / 180;

            //基于偏移量定义起始点
            double x = radius + (radius - 5) * Math.Cos(offsetX);
            double y = radius + (radius - 5) * Math.Sin(offsetY);

            //中间--决定绘制大弧还是小弧
            int Is50 = PercentValue < 50 ? 0 : 1;
            //移动路径--字符串
            string pathStr = $"M{radius + 0.01} 5A{radius - 5} {radius - 5} 0 {Is50} 0 {x} {y}";
            //A radiusX,RadiusY degrees isLargeArc,isClockwise x,y：创建一条到指定点的 ArgSegment线段。指定描述弧线的椭圆半径、弧线旋转的度数，以及用于设置前面介绍的IsLargeArc和SweepDirection属性的布尔标志

            //将路径转换为几何图像
            var converter = TypeDescriptor.GetConverter(typeof(Geometry));
            path.Data = (Geometry)converter.ConvertFrom(pathStr);
        }
    }
}
